﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using compshop.Infrastructure;
using compshop.Infrastructure.CompShopDataService;
namespace compshop.Modules.ListOfEmployees.Presenter
{
  public  class EmployeeListPresenter
    {
        private CompShopEntities compShopDataContext;
        public IViewEployeeList View { set; get; }
        private EventsManager eventsManager;
        public EmployeeListPresenter( EventsManager eventsManager)
        {
            compShopDataContext= CompShopEntitiesContext.GetInstance();
            EmployeeList = compShopDataContext.Employees.ToList();
            this.eventsManager = eventsManager;
            eventsManager.Presenter = this;
            eventsManager.Subscribe();
        }

        public List<Employee> EmployeeList { set; get; }
        public void AddEmployee()
        {
            EditEmployeeView editWindow = new EditEmployeeView();
            editWindow.ShowDialog();
            if (editWindow.DialogResult == true)
            {
                compShopDataContext.AddToEmployees(editWindow.employee);
                compShopDataContext.SaveChanges();
                EmployeeList.Add(editWindow.employee);
            }
        }
       public void EditEmployee(Employee editingEmployee)
        {
            EditEmployeeView editWindow = new EditEmployeeView(editingEmployee);
            editWindow.ShowDialog();
            if (editWindow.DialogResult == true)
            {
                compShopDataContext.UpdateObject(editWindow.employee);
                compShopDataContext.SaveChanges();
               
            }
        }
       #region Publishers

       public void EmployeeSelectedPublisher(int id)
       {
           eventsManager.eventAggregator.GetEvent<EmployeeSelectedEvent>().Publish(id);
       }

       #endregion

       #region Handlers

       public void EmployeeSelectedEventHandler(int id)
       {
           View.SelectedEmployee(id);
       }

       #endregion

    }
}
